import React, { useState, useImperativeHandle, forwardRef, ReactNode } from 'react';
import { Modal } from 'antd';

interface CustomModalProps {
  title: string;
  children: ReactNode;
  onConfirm: () => void;
}

export interface CustomModalHandles {
  openModal: () => void;
  closeModal: () => void;
}

const CustomModal = forwardRef<CustomModalHandles, CustomModalProps>(
  ({ title, children,onConfirm }, ref) => {
    const [isVisible, setIsVisible] = useState(false);

    const showModal = () => {
      setIsVisible(true);
    };

    const hideModal = () => {
      setIsVisible(false);
    };

    useImperativeHandle(ref, () => ({
      openModal: showModal,
      closeModal: hideModal,
    }));

    return (
      <Modal
        title={title}
        open={isVisible}
        onOk={onConfirm}
        onCancel={hideModal}
        okText="确认"
        cancelText="取消"
      >
        {children}
      </Modal>
    );
  }
);

export default CustomModal;